//
//  MyContactListener.cpp
//  Demo
//
//  Created by CG on 13-10-8.
//
//

#include "MyContactListener.h"
#include "Utils.h"
#include "cocos2d.h"
using namespace cocos2d;

MyContactListener::MyContactListener()
:_contacts()
{

}


MyContactListener::~MyContactListener()
{
}

void MyContactListener::BeginContact(b2Contact *contact)
{
    // We need to copy out the data because the b2Contact passed in
    // is reused.
    MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
    _contacts.push_back(myContact);
}


void MyContactListener::EndContact(b2Contact* contact) {
    MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
    std::vector<MyContact>::iterator pos;
    pos = std::find(_contacts.begin(), _contacts.end(), myContact);
    if (pos != _contacts.end()) {
        
        //wcg
        //handle collision
        this->HandleCollision(myContact);
        //end
        _contacts.erase(pos);
    }
}

void MyContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) {
}

void MyContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {
}


void MyContactListener::HandleCollision(MyContact &myContact)
{
    void* obj1 = myContact.fixtureA->GetBody()->GetUserData();
    void* obj2 = myContact.fixtureB->GetBody()->GetUserData();
    if(obj1 && obj2)
    {
        BaseObject* firstObj = (BaseObject*)obj1;
        BaseObject* secondObj = (BaseObject*)obj2;
        
        //collision:  ball and bucket
        if ((firstObj->getTag() == kBallTag && secondObj->getTag() == kBucketTag) ||
            (firstObj->getTag() == kBucketTag && secondObj->getTag() == kBallTag) )
        {
            CCLOG("A: %d  B: %d", firstObj->getTag(), secondObj->getTag());

            //the same color
            if (firstObj->GetObjectType() ==  secondObj->GetObjectType())
            {
                //get the ball speed
                b2Vec2 ballSpeed;
                Ball* ballObj;
                Bucket* bucketObj;
            
                
                if (firstObj->GetObjectClassification() == ObjectClassification_Ball)
                {
                    ballObj = (Ball*) firstObj;
                    bucketObj = (Bucket*)secondObj;
                    ballSpeed = myContact.fixtureA->GetBody()->GetLinearVelocity();
                }
                else if(secondObj->GetObjectClassification() == ObjectClassification_Ball)
                {
                    ballObj = (Ball*) secondObj;
                    bucketObj = (Bucket*)firstObj;
                    ballSpeed = myContact.fixtureB->GetBody()->GetLinearVelocity();
                }
                
                CCLOG("OKOKOKOKOKOKOKOKOKOKOKOKOK: %f    %f",ballSpeed.x, ballSpeed.y );
                //
                float stillSpeed = 0.7f;
                if (fabs(ballSpeed.x) < stillSpeed && fabs(ballSpeed.y) < stillSpeed)
                {
                    //stop the ball
                    ballObj->GetBody()->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
                    
                    //ball is still in bucket
                    firstObj->PlayCollisionEffect();
                    secondObj->PlayCollisionEffect();
                }
            }
        }
    }
}